IF exists(SELECT * FROM dbo.sysobjects WHERE name='hulpLlnPerKlas' AND xtype='P') 
BEGIN
	DROP PROCEDURE hulpLlnPerKlas
END
GO

CREATE procedure hulpLlnPerKlas
as
set nocount on

select '0AAAA' as klas,0 as klasNr,'<?xml version="1.0"  encoding="ISO-8859-1" ?><leerlingen>' as tekst
union
select distinct ltrim(rtrim(klas)),0,'<klas klasNaam = "' + ltrim(rtrim(upper(klas))) + '">'
from leerlingen..leerlingen
union
select klas,klasnr,'<lln iNr="' + l.inschrijvingsNr + '"><naam>'  
	+ isNull(voornaam,'') + ' ' + isNull(achternaam,'') + '</naam></lln>'
from leerlingen..leerlingen as l
union
select distinct ltrim(rtrim(klas)),999,'</klas>'
from leerlingen..leerlingen
union
select 'ZZZZZ',0,'</leerlingen>'
order by klas, klasNr